package cn.iocoder.yudao.module.system.controller.admin.cameraevent;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.cameraevent.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.cameraevent.CameraEventDO;
import cn.iocoder.yudao.module.system.convert.cameraevent.CameraEventConvert;
import cn.iocoder.yudao.module.system.service.cameraevent.CameraEventService;

@Api(tags = "管理后台 - 告警异常列表")
@RestController
@RequestMapping("/system/camera-event")
@Validated
public class CameraEventController {

    @Resource
    private CameraEventService cameraEventService;

    @PostMapping("/create")
    @ApiOperation("创建告警异常列表")
    @PreAuthorize("@ss.hasPermission('system:camera-event:create')")
    public CommonResult<Long> createCameraEvent(@Valid @RequestBody CameraEventCreateReqVO createReqVO) {
        return success(cameraEventService.createCameraEvent(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新告警异常列表")
    @PreAuthorize("@ss.hasPermission('system:camera-event:update')")
    public CommonResult<Boolean> updateCameraEvent(@Valid @RequestBody CameraEventUpdateReqVO updateReqVO) {
        cameraEventService.updateCameraEvent(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除告警异常列表")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('system:camera-event:delete')")
    public CommonResult<Boolean> deleteCameraEvent(@RequestParam("id") Long id) {
        cameraEventService.deleteCameraEvent(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得告警异常列表")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('system:camera-event:query')")
    public CommonResult<CameraEventRespVO> getCameraEvent(@RequestParam("id") Long id) {
        CameraEventDO cameraEvent = cameraEventService.getCameraEvent(id);
        return success(CameraEventConvert.INSTANCE.convert(cameraEvent));
    }

    @GetMapping("/list")
    @ApiOperation("获得告警异常列表列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('system:camera-event:query')")
    public CommonResult<List<CameraEventRespVO>> getCameraEventList(@RequestParam("ids") Collection<Long> ids) {
        List<CameraEventDO> list = cameraEventService.getCameraEventList(ids);
        return success(CameraEventConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得告警异常列表分页")
    @PreAuthorize("@ss.hasPermission('system:camera-event:query')")
    public CommonResult<PageResult<CameraEventRespVO>> getCameraEventPage(@Valid CameraEventPageReqVO pageVO) {
        PageResult<CameraEventDO> pageResult = cameraEventService.getCameraEventPage(pageVO);
        return success(CameraEventConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出告警异常列表 Excel")
    @PreAuthorize("@ss.hasPermission('system:camera-event:export')")
    @OperateLog(type = EXPORT)
    public void exportCameraEventExcel(@Valid CameraEventExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<CameraEventDO> list = cameraEventService.getCameraEventList(exportReqVO);
        // 导出 Excel
        List<CameraEventExcelVO> datas = CameraEventConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "告警异常列表.xls", "数据", CameraEventExcelVO.class, datas);
    }

}
